package businessObjects;

import java.sql.SQLException;
import java.util.Date;

import connection.ConnectionDAO;
import connection.JDCConnection;

import dao.FactureDAO;

import model.Facture;

public class FactureBo {
	private FactureDAO dao;
	private ConnectionDAO con;
	
	public FactureBo() {
		dao = new FactureDAO();
		con = new ConnectionDAO();
	}
	
	/**
	 * Genere une facture et sauvegarde dans la base de donnees
	 * @param idEtudiant
	 * @param details
	 * @param montant
	 * @return Retourne true si succes ou false si echec
	 * @throws SQLException 
	 */
	public boolean genererFacture(int idEtudiant, String details, double montant) throws SQLException {
		Facture facture = new Facture();
		facture.setIdEtudiant(idEtudiant);
		facture.setDetails(details);
		facture.setMontant(montant);
		JDCConnection c = con.getConnection();
		boolean valid = dao.saveFacture(c, facture);
		if(valid) {
			c.commit();
		}
		else {
			c.rollback();
		}
		c.close();
		return valid;
	}
	
	/**
	 * Retourne la facure associe a un etudiant
	 * @param idEtudiant
	 * @return Retourne un objet Facture
	 * @throws SQLException 
	 */
	public Facture getFacture(int idEtudiant) throws SQLException {
		JDCConnection c = con.getConnection();
		Facture fct = dao.getFacturebyIdEtudiant(c, idEtudiant);
		c.close();
		return fct;
	}
	
	public boolean payerFacture(int idFacture, int numConfirmation) throws SQLException{
		JDCConnection c = con.getConnection();
		boolean valid = dao.payerFacture(c, idFacture, numConfirmation, new Date().getTime());
		if(valid) {
			c.commit();
		}
		else {
			c.rollback();
		}
		c.close();
		return valid;
	}
	
}
